package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.LawsRegulationsAddDTO;
import org.jsola.hr.dto.LawsRegulationsUpdateDTO;
import org.jsola.hr.query.LawsRegulationsQuery;
import org.jsola.hr.service.ILawsRegulationsService;
import org.jsola.hr.vo.LawsRegulationsListVO;
import org.jsola.hr.vo.LawsRegulationsVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 卫人云-政策法规
 *
 * @author zhr
 */
@RestController("hrLawsRegulationsController")
@RequestMapping(HrConstants.API_PATH + "/v1/lawsRegulations")
public class LawsRegulationsController implements ILawsRegulationsControllerAPI {

    @Autowired
    private ILawsRegulationsService lawsRegulationsService;

    @Override
    @PostMapping
    public Result<LawsRegulationsVO> save(@Valid @RequestBody LawsRegulationsAddDTO lawsRegulationsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增卫人云-政策法规权限校验

        return Result.success(
                lawsRegulationsService.save(lawsRegulationsAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody LawsRegulationsUpdateDTO lawsRegulationsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改卫人云-政策法规权限校验

        int count = lawsRegulationsService.update(lawsRegulationsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{lawsRegulationsId}")
    public Result<Integer> delete(@PathVariable String lawsRegulationsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除卫人云-政策法规权限校验

        int count = lawsRegulationsService.deleteByIds(tokenUser, lawsRegulationsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] lawsRegulationsIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除卫人云-政策法规权限校验

        int count = lawsRegulationsService.deleteByIds(tokenUser, lawsRegulationsIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{lawsRegulationsId}")
    public Result<LawsRegulationsVO> findById(@PathVariable String lawsRegulationsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看卫人云-政策法规权限校验

        return Result.success(
            lawsRegulationsService.selectById(lawsRegulationsId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<LawsRegulationsListVO>> listAll(@Valid LawsRegulationsQuery lawsRegulationsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看卫人云-政策法规权限校验

        return Result.success(
            lawsRegulationsService.select(lawsRegulationsQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<LawsRegulationsListVO>> page(@Valid LawsRegulationsQuery lawsRegulationsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            lawsRegulationsService.selectPage(lawsRegulationsQuery, tokenUser.getSiteId())
        );
    }


}

